package com.pi.base.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pi.base.common.BasePage;
import com.pi.base.entity.SysRole;
import com.pi.base.entity.SysRoleExample;
import com.pi.base.entity.SysRoleMenu;
import com.pi.base.entity.SysRoleMenuExample;
import com.pi.base.mapper.BaseSysRoleMapper;
import com.pi.base.mapper.SysMenuMapper;
import com.pi.base.mapper.SysRoleMapper;
import com.pi.base.mapper.SysRoleMenuMapper;
import com.pi.base.service.BaseService;
import com.pi.base.service.ISysRoleService;
import com.pi.base.util.MapBuilder;
import com.pi.base.util.PropKit;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("sysRoleService")
public class SysRoleService extends BaseService<SysRole> implements ISysRoleService{
	
	private final Logger logger = LoggerFactory.getLogger(SysRoleService.class);
			
	@Autowired
	private SysMenuMapper sysMenuMapper;
	@Autowired
	private SysRoleMapper sysRoleMapper;
	@Autowired
	private BaseSysRoleMapper baseSysRoleMapper ;
	@Autowired
	private SysRoleMenuMapper sysRoleMenuMapper;

	@Override
	public SysRole getRoleById(int roleId) throws Exception {
		return sysRoleMapper.selectByPrimaryKey(roleId);
	}
	

	@Override
	public BasePage<SysRole> getRoleListByExample(SysRoleExample example,
			int page, int rows) throws Exception {
		Page<SysRole> pageInfo = PageHelper.startPage(page,rows);
		List<SysRole> roleList = sysRoleMapper.selectByExample(example);
		return new BasePage<SysRole>(pageInfo.getTotal(),roleList);
	}

	@Override
	public boolean insertRole(SysRole newRole, String roleMenuIds)
			throws Exception {
		PropKit.use("tableMap.properties");
		String table_sys_menu = PropKit.get("sys_role","sys_role");
		Map<String, Object> params = new MapBuilder.Builder<Object>().
				map("sysRole",newRole).
				map("tableName",table_sys_menu).build().map();
		 baseSysRoleMapper.insertRoleReturnId(params);
	//	logger.debug("roleId:"+roleId);
		 if(params.get("id") == null){
			 throw new Exception();
		 }
		 newRole.setId(Integer.parseInt(params.get("id").toString()));
		String[] menuIds = roleMenuIds.split(",");
		for (String menuId : menuIds) {
			SysRoleMenu roleMenu = new SysRoleMenu();
			roleMenu.setMenuid(Integer.valueOf(menuId));
			roleMenu.setRoleid(newRole.getId());
			sysRoleMenuMapper.insertSelective(roleMenu);
		}
		return true;
		
	}



	@Override
	public String getButtonCodeStrsByRoleId(Integer roleId) throws Exception {
		PropKit.use("tableMap.properties");
		String table_sys_menu = PropKit.get("sys_menu","sys_menu");
		Map<String, Object> params = new MapBuilder.Builder<Object>().
				map("roleId",roleId).
				map("tableName",table_sys_menu).
				map("tableSysRoleMenu","sys_role_menu").
				map("tableSysRoleMenubuild",PropKit.get("sys_role_menu","sys_role_menu")).
				build().map();
		return baseSysRoleMapper.queryButtonCodeStrsByRoleId(params);
	}

	@Override
	public boolean updateRoleStateByRoleIds(List<Integer> roleIds, int roleState)
			throws Exception {
		SysRoleExample roleExample = new SysRoleExample();
		roleExample.createCriteria().andIdIn(roleIds);
		SysRole updateRole = new SysRole();
		updateRole.setState(roleState);
		return sysRoleMapper.updateByExampleSelective(updateRole, roleExample) > 0;
	}


	@Override
	public String queryMenuIdStrsByRoleId(Integer roleId) throws Exception {
		PropKit.use("tableMap.properties");
		Map<String, Object> params = new MapBuilder.Builder<Object>().
				map("roleId",roleId).
				map("tableName",PropKit.get("sys_role_menu","sys_role_menu")).
				build().map();
		return baseSysRoleMapper.queryMenuIdStrsByRoleId(params);
	}


	@Override
	public boolean deleteRole(List<Integer> removeRoleId) throws Exception {
		//1.删除角色
		SysRoleExample example = new SysRoleExample();
		example.createCriteria().andIdIn(removeRoleId);
		sysRoleMapper.deleteByExample(example);
		//2.删除角色菜单对应关系
		SysRoleMenuExample menuExample = new SysRoleMenuExample();
		menuExample.createCriteria().andRoleidIn(removeRoleId);
		sysRoleMenuMapper.deleteByExample(menuExample);
		return true;
	}


	@Override
	public boolean updateRole(SysRole updatedRole, String updatedRoleMenuIds)
			throws Exception {
		//1.更新角色
		sysRoleMapper.updateByPrimaryKeySelective(updatedRole);
		//2.删除以前的角色菜单关系
		SysRoleMenuExample roleMenuExample = new SysRoleMenuExample();
		roleMenuExample.createCriteria().andRoleidEqualTo(updatedRole.getId());
		sysRoleMenuMapper.deleteByExample(roleMenuExample);
		//3.新增新的角色菜单关系
		String[] menuIds = updatedRoleMenuIds.split(",");
		SysRoleMenu temp;
		for (String menuId : menuIds) {
			temp = new SysRoleMenu();
			temp.setMenuid(Integer.valueOf(menuId));
			temp.setRoleid(updatedRole.getId());
			sysRoleMenuMapper.insertSelective(temp);
		}
		return true;
	}
}
